package com.sino.androiddesign.database.nativeDataBase

import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import android.util.Log

class SQLiteUtils{


    companion object {
        val TAG = "SQLiteUtils"

         val dataBase:SQLiteDatabase = SQLiteDbHelper.instance.writableDatabase


        //原生数据库 2: 增删改查的相应的操作
        fun  addStudent( student: Student){
            val contentValues = ContentValues()
            Log.e(TAG,"${student.name},${student.desk}")
            contentValues.put("name",student.name)
            contentValues.put("cls",student.desk)
            val insert = dataBase.insert(SQLiteDbHelper.table_student, null, contentValues)
           if (insert.toInt() === -1){
               Log.e(TAG,"插入失败")
           }else{
               Log.e(TAG,"插入成功")
           }
        }

        fun queryStudent(name:String){
            val strings:Array<String> = arrayOf(name)
            val cursor = dataBase.query(SQLiteDbHelper.table_student, null, "name = ?", strings, null, null, null, null)

            while (cursor.moveToNext()){
                val stuName = cursor.getString(cursor.getColumnIndex("name"))
                val stuCls = cursor.getString(cursor.getColumnIndex("cls"))
                Log.e(TAG,"name=>$stuName;class => $stuCls")
            }
            cursor.close()
        }

        fun updateStudent(cls:String,name: String){
            val contentValues = ContentValues()
            contentValues.put("cls",cls)
            val update = dataBase.update(SQLiteDbHelper.table_student, contentValues, "name = ?", arrayOf(name))
            Log.e(TAG,"受影响的数量 $update")
        }

        fun  deleteStudent(name: String){
            val delete = dataBase.delete(SQLiteDbHelper.table_student, "name = ?", arrayOf(name))
            Log.e(TAG,"删除的个数  $delete")
        }
    }
}